//
//  RegisterViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RegisterViewController.h"
#import "Masseage.h"
#import "aStageHeader.pch"
@interface RegisterViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *zhanghaoTextField;
@property (nonatomic, retain) UITextField *pwdTextField;
@property (nonatomic, retain) UITextField *querenTextField;
@property (nonatomic, retain) UITextField *nameTextField;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *zhanghaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 50, 30)];
    zhanghaoLabel.text = @"账号";
    [self.view addSubview:zhanghaoLabel];
    UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 160, 50, 30)];
    pwdLabel.text = @"密码";
    [self.view addSubview:pwdLabel];
    UILabel *qurenLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 220, 80, 30)];
    qurenLabel.text = @"确认密码";
    [self.view addSubview:qurenLabel];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 280, 50, 30)];
    nameLabel.text = @"昵称";
    [self.view addSubview:nameLabel];
    self.zhanghaoTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
    _zhanghaoTextField.placeholder = @"请输入账号";
    [self.view addSubview:_zhanghaoTextField];
    _zhanghaoTextField.borderStyle = UITextBorderStyleRoundedRect;
    _zhanghaoTextField.tag = 1000;
    _zhanghaoTextField.delegate = self;
    self.pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 200, 30)];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.tag = 1001;
    _pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    _pwdTextField.delegate = self;
    [self.view addSubview:_pwdTextField];
    self.querenTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 220, 200, 30)];
    _querenTextField.placeholder = @"请确认密码";
    _querenTextField.tag = 1002;
    _querenTextField.borderStyle = UITextBorderStyleRoundedRect;
    _querenTextField.delegate = self;
    [self.view addSubview:_querenTextField];
    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 280, 200, 30)];
    _nameTextField.placeholder = @"请输入昵称";
    _nameTextField.tag = 1003;
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    _nameTextField.delegate = self;
    [self.view addSubview:_nameTextField];
    UIButton *querenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    querenButton.layer.cornerRadius = 3;
    querenButton.clipsToBounds = YES;
    querenButton.backgroundColor = [UIColor redColor];
    querenButton.frame = CGRectMake(120, 335, 100, 30);
    [querenButton setTitle:@"注册" forState:UIControlStateNormal];
    [querenButton addTarget:self action:@selector(completed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:querenButton];
}
- (void)completed
{
    
    [[DataBaseManager shareInstance] openDB];
    // 创建表
    [[DataBaseManager shareInstance] creatTabel];
    if(_zhanghaoTextField.text.length != 0 && _pwdTextField.text.length != 0 && _querenTextField.text.length != 0 &&_nameTextField.text.length != 0){
        if (![_pwdTextField.text isEqualToString:_querenTextField.text]) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不一致" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                //
            }];
            [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer) userInfo:nil repeats:NO];
        } else {
            Masseage *m = [[Masseage alloc] init];
            m.mail = _zhanghaoTextField.text;
            m.pwd = _pwdTextField.text;
            m.chepwd = _querenTextField.text;
            m.name = _nameTextField.text;
            NSMutableArray *arr = [[DataBaseManager shareInstance] select];
            for (Masseage *model in arr) {
                if ([model.mail isEqualToString:m.mail]) {
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"邮箱已存在" preferredStyle:UIAlertControllerStyleAlert];
                    [self presentViewController:alert animated:YES completion:^{
                        //
                    }];
                    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:NO];
                    return;// 直接返回
                }
            }
            // 插入数据
            [[DataBaseManager shareInstance] insertMassage:m];
            // 显示注册成功
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertVC animated:YES completion:nil];
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:NO];
            [self dismissViewControllerAnimated:YES completion:^{
                //
            }];
        }
    }
}
- (void)timer
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
